import {RemUtil} from "./utils/RemUtil";
import {createApp, ref} from 'vue'
import App from './App.vue'
import router from './router/index';
import "./axios/index";
import ElementPlus from 'element-plus';
import 'element-plus/lib/theme-chalk/index.css';
import 'dayjs/locale/zh-cn'
import zhLocale from 'element-plus/lib/locale/lang/zh-cn'

new RemUtil();
const app = createApp(App);
app.use(router)
    .use(ElementPlus)
    .mount('#app');
//中文化处理
const ElementPlus1: any = ElementPlus;
ElementPlus1.useLang = (app: any, ref: any, locale: any) => {
    const template = (str: any, option: any) => {
        if (!str || !option) return str
        return str.replace(/\{(\w+)\}/g, (_: any, key: any) => {
            return option[key]
        })
    }

    // 注入全局属性,子组件都能通过inject获取
    app.provide('ElLocaleInjection', {
        lang: ref(locale.name),
        locale: ref(locale),
        t: (...args: any) => {
            const [path, option] = args
            let value
            const array = path.split('.')
            let current = locale
            for (let i = 0, j = array.length; i < j; i++) {
                const property = array[i]
                value = current[property]
                if (i === j - 1) return template(value, option)
                if (!value) return ''
                current = value
            }
        },
    })
}
ElementPlus1.useLang(app, ref, zhLocale)